{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16d88550-27ea-4e52-94fa-5ade909ed00a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import usb.core\n",
    "import usb.util\n",
    "import usb.control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ddc89fe-dff2-464c-b1a4-dbd00ceab101",
   "metadata": {},
   "outputs": [],
   "source": [
    "dev = usb.core.find(idVendor=0x10a5, idProduct=0x9201)\n",
    "dev.reset()\n",
    "cfg = dev.get_active_configuration()\n",
    "intf = cfg[(0,0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3c6a46d-951e-440f-9d61-5a4a45231277",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(intf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0d3edb9-446a-4d91-a057-533b5e7b9d0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def match(e):\n",
    "    return usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN\n",
    "ep = usb.util.find_descriptor(intf, custom_match=match)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cea804bf-707e-4ce5-9505-f4721fddd43c",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret = dev.ctrl_transfer(\n",
    "    bmRequestType=0x40,\n",
    "    bRequest=8,\n",
    "    wValue=0x0010,\n",
    "    wIndex=0x0000,\n",
    "    data_or_wLength=0\n",
    ")\n",
    "print(ret)\n",
    "ret = dev.ctrl_transfer(\n",
    "    bmRequestType=0xc0,\n",
    "    bRequest=80,\n",
    "    wValue=0x0000,\n",
    "    wIndex=0x0000,\n",
    "    data_or_wLength=72\n",
    ")\n",
    "print(f'Version {ret[0]}.{ret[1]}.{ret[2]}.{ret[3]}')\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93b7a83c-fa1b-4f70-b70c-11f40367fda3",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret = dev.ctrl_transfer(\n",
    "    bmRequestType=0x40,\n",
    "    bRequest=1,\n",
    "    wValue=0x0001,\n",
    "    wIndex=0x0000,\n",
    "    data_or_wLength=bytearray.fromhex('102f1117')\n",
    ")\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0324f339-804e-4f88-bae8-8c737393cc4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ep.read(64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "327d1d2f-f3e3-43df-aafd-c5ccd6db1d16",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret = dev.ctrl_transfer(\n",
    "    bmRequestType=0xc0,\n",
    "    bRequest=11,\n",
    "    wValue=0x0000,\n",
    "    wIndex=0x0000,\n",
    "    data_or_wLength=1000\n",
    ")\n",
    "print(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77294cc0-fc6d-43e6-9205-3241e654a53d",
   "metadata": {},
   "outputs": [],
   "source": [
    "ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2121b581-ea6c-427e-9e90-f0a4ef15b848",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
